console.log("我是个人情况页面")
var roleMap = {
	0:"管理员",
	1:"院士",
	2:"人才",
	3:"单位",
	4:"组织单位",
	5:"专家"
};
var statusMap={
	1:"启用",
	2:"禁用"
};

layui.use(['form', 'code', 'laydate','upload','layer','laytpl','element','laypage'], function() {
	var laydate = layui.laydate;
	form = layui.form;
	upload = layui.upload;
	layer = layui.layer;
	laytpl = layui.laytpl;
	element = layui.element;
	laypage = layui.laypage;



	layui.code();
	//初始化列表
//初始化列表
	setTimeout(search,500);
	$(".search").click(function () {
		console.log("haha");
		search();
		return false;
	});
	function search(){
		$(".userList").html("");
		let searchAccount = $(".searchAccount").val();

		AJAX.post(cgiMain.userList,
		{pageNum:1,pageSize:10,searchAccount:searchAccount},
		function (res) {

			if(res.code == '0000'){
				$(".userList").data("userList",res.result.list);
				var resultPage = res.result;
				var tpl = $("#userTpl").html();
				for(var i = 0; i < resultPage.list.length;i++){
					var item = resultPage.list[i];
					if(item.sex == 1){
						item.sexName = "男";
					}else if(item.sex == 2){
						item.sexName = "女";
					}else{
						item.sexName = "注册用户";
					}
					item.roleId = item.role.id;
					item.roleName = roleMap[item.roleId];
					item.statusName=statusMap[item.status];

					laytpl(tpl).render(item,function (html) {
						$(".userList").append(html);
						$(".userList").on("click",'.startOrStop',function (elem) {
							member_stop(elem);

						});
						$(".userList").on("click",'.deleteUser',function (elem) {
							member_del(elem);
						});
						$(".userList").on("click",'.editUser',function (elem) {
							member_edit(elem);
						});
					});
				}
				//执行一个laypage实例
				laypage.render({
					elem: 'pagination'
					,limit: 10
					,count: resultPage.totalCount
					,limits:[10]
					,layout:['prev', 'page', 'next','limit','count']
					,jump: function (obj,first) {
						if(first == true){
							return;
						}
						$(".userList").html("");
						nextPage(obj.curr,10);

					}
				});
				element.render(".userList");
				form.render();
			}

		});
	}

	function nextPage(pageNum,pageSize){
		let searchAccount = $(".searchAccount").val();
		AJAX.post(cgiMain.userList,
			{pageNum:pageNum,pageSize:pageSize,searchAccount:searchAccount},
			function (res) {

				if(res.code == '0000'){
					$(".userList").data("userList",res.result.list);
					var resultPage = res.result;
					var tpl = $("#userTpl").html();
					for(var i = 0; i < resultPage.list.length;i++){
						var item = resultPage.list[i];
						if(item.sex == 1){
							item.sexName = "男";
						}else if(item.sex == 2){
							item.sexName = "女";
						}else{
							item.sexName = "注册用户";
						}
						item.roleId = item.role.id;
						item.roleName = roleMap[item.roleId];
						item.statusName=statusMap[item.status];

						laytpl(tpl).render(item,function (html) {
							$(".userList").append(html);
							$(".userList").on("click",'.startOrStop',function (elem) {
								member_stop(elem);

							});
							$(".userList").on("click",'.deleteUser',function (elem) {
								member_del(elem);
							});
							$(".userList").on("click",'.editUser',function (elem) {
								member_edit(elem);
							});
						});
					}

					element.render(".userList");
					form.render();
				}

			});
	}


	// 添加工作经验
	$(".add_btn").on("click", function() {
		$("#myModal").show();
		$("#myModal input, #myModal textarea").val("");
	});

	$("#submitNewMember").click(function () {
		// 判断必填项--提示--统校验
		var params = {};

		// 集合所有参数
		$("#addNewMemberForm .save_val").each(function() {
			var name = $(this).attr("name");
			var val = $(this).val();
			params[name] = val;
		});

		var url = cgiMain.userAdd;
		if(params.id && params.id > 0){
			url = cgiMain.userEdit;
		}


		AJAX.post(url,
			params,
			function (res) {
				if(res.code=='0000'){
					layer.msg('保存成功');
					$("#myModal").hide();
					window.location.reload();
				}
			});
	});

	$("#cancelAddNewMember").on("click",function () {
		$("#myModal").hide();
		$("#myModal input, #myModal textarea").val("");
	});
	$(".close").on("click",function () {
		$("#myModal").hide();
		$("#myModal input, #myModal textarea").val("");
	});


	/*用户-停用*/
	function member_stop(dom){
		var userId = $(dom.target).attr("data-id");
		var userList = $(".userList").data("userList");

		var user={};
		for(var i = 0 ; i < userList.length;i++){
			if(userList[i].id == userId){
				user = userList[i];
				break;
			}
		}
		layer.confirm('确认要停用吗？',function(index){
			// user.status = user.status==1?2:1;
			// user.role=null;
			AJAX.post(cgiMain.userEdit,
				{id:user.id,status:user.status==1?2:1,roleId:user.roleId},
				function (res) {
					if(res.code=='0000'){
						window.location.reload();
					}
				});
		});
	}
	/*用户-停用*/
	function member_edit(dom){
		var userId = $(dom.target).attr("data-id");
		var userList = $(".userList").data("userList");

		var user={};
		for(var i = 0 ; i < userList.length;i++){
			if(userList[i].id == userId){
				user = userList[i];
				break;
			}
		}

		$(".account").val(user.account);
		$(".sex").val(user.sex);
		$(".id").val(user.id);
		$(".phone").val(user.phone);
		$(".email").val(user.email);
		$(".roleId").val(user.roleId);

		form.render();
		form.render('select');

		$("#myModal").show();

	}

	/*用户-删除*/
	function member_del(dom){
		var userId = $(dom.target).attr("data-id");
		layer.confirm('确认要删除吗？',function(index){
			AJAX.post(cgiMain.userDelete,
				{id:userId},
				function (res) {
					if(res.code=='0000'){
						window.location.reload();
					}
				});
		});
	}



	function delAll (argument) {
		var ids = [];

		// 获取选中的id
		$('tbody input').each(function(index, el) {
			if($(this).prop('checked')){
				ids.push($(this).val())
			}
		});

		layer.confirm('确认要删除吗？'+ids.toString(),function(index){
			//捉到所有被选中的，发异步进行删除
			layer.msg('删除成功', {icon: 1});
			$(".layui-form-checked").not('.header').parents('tr').remove();
		});
	}
});
